package cn.com.pet.administrator.action.encyclopedia;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.pet.common.base.BaseAction;
import cn.com.pet.common.entity.PetType;
import cn.com.pet.common.service.PetTypeService;
import cn.com.pet.common.util.GuidUtil;
import cn.com.pet.common.util.StringUtil;

@Namespace("/action")
@ParentPackage("base")
@SuppressWarnings({ "serial", "unused" })
public class PetTypeAction extends BaseAction {

	@Autowired
	PetTypeService petTypeService;

	@Action(value = "pageAddPetType", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/addPetType.jsp") })
	public String pageAddPetType() {
		return SUCCESS;
	}

	@Action(value = "pageUpdatePetType", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/updatePetType.jsp") })
	public String pageUpdatePetType() {
		String petTypeId = this.getRequest().getParameter("id");
		this.getRequest().setAttribute("id", petTypeId);
		return SUCCESS;
	}

	@Action(value = "addPetType", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petTypeList.jsp") })
	public String addPetType() {

		PetType pt = new PetType();
		pt.setCreateDate(new Date());
		pt.setPetDescription(this.getRequest().getParameter("description"));
		// pt.setPetTypeId(GuidUtil.createNewGuid(GuidUtil.FormatString));
		pt.setPetTypeName(this.getRequest().getParameter("name"));
		pt.setStatus(1);
		pt.setSubHeadingId(this.getRequest().getParameter("subid"));
		pt.setUpdateDate(new Date());

		int result = petTypeService.addPetType(pt);
		return SUCCESS;
	}

	@Action(value = "deletePetType", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petTypeList.jsp") })
	public String deletePetType() {

		PetType pt = new PetType();

		pt.setPetTypeId(StringUtil.parsLong(this.getRequest().getParameter("id")));

		int result = petTypeService.deletePetType(pt);

		return SUCCESS;
	}

	@Action(value = "updatePetType", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petTypeList.jsp") })
	public String updatePetType() {

		PetType pt = new PetType();
		pt.setPetDescription(this.getRequest().getParameter("description"));
		pt.setPetTypeId(StringUtil.parsLong(this.getRequest().getParameter("id")));
		pt.setPetTypeName(this.getRequest().getParameter("name"));
		pt.setStatus(StringUtil.parsInt(this.getRequest().getParameter("status")));
		pt.setSubHeadingId(this.getRequest().getParameter("subid"));
		pt.setUpdateDate(new Date());

		int result = petTypeService.updatePetType(pt);

		return SUCCESS;
	}

	@Action(value = "findAllPetType", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petTypeList.jsp") })
	public String findAllPetType() {

		Map<String, String> parameter = new HashMap<String, String>();

		List<PetType> petTypeList = petTypeService.findAllPetType(parameter);
		this.getRequest().setAttribute("allPetType", petTypeList);

		return SUCCESS;
	}

	@Action(value = "findPetTypeById", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petTypeDetail.jsp") })
	public String findPetTypeById() {

		PetType pt = new PetType();

		pt.setPetTypeId(StringUtil.parsLong(this.getRequest().getParameter("id")));

		PetType petType = petTypeService.findPetTypeById(pt);

		this.getRequest().setAttribute("petType", petType);

		return SUCCESS;
	}
}
